﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using HYRevitCode.XYZUtility;
using YJKRGeometry;
using QuickModel.DoorAndWindow;
using Teigha.Core;
using Teigha.TD;
using Transfer.Utils;

namespace QuickModel.InteractiveOperation
{
	public class GeometryCommon
	{
		public static XYZ ConvetPointFromCadToRevit(OdGePoint3d cadPnt, Transform trans = null)
		{
			XYZ result;
			try
			{
				XYZ xyz = new XYZ(cadPnt.x, cadPnt.y, cadPnt.z) / 304.8;
				bool flag = trans == null;
				if (flag)
				{
					result = xyz;
				}
				else
				{
					result = trans.OfPoint(xyz);
				}
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		public static XYZ GetDensePointsSideDir(List<XYZ> lstPoint, XYZ pntBase, XYZ vcDivide, double dTol = 1.0)
		{
			XYZ xyz = XYZUtilityMethod.RotateTo(vcDivide, Math.PI*.5, XYZ.BasisZ);
			dTol = Common.MMToFeet(dTol);
			int num = 0;
			foreach (XYZ xyz2 in lstPoint)
			{
				XYZ xyz3 = xyz2 - pntBase;
				bool flag = xyz3.IsZeroLength();
				if (!flag)
				{
					double num2 = xyz3.AngleTo(xyz);
					double val = xyz3.GetLength() * Math.Abs(Math.Sin(num2));
					bool flag2 = YJKRGeometry.Geometry.Lessthan_Or_Equal(val, dTol);
					if (!flag2)
					{
						bool flag3 = YJKRGeometry.Geometry.LessThan(num2, Math.PI*.5);
						if (flag3)
						{
							num++;
						}
						else
						{
							num--;
						}
					}
				}
			}
			bool flag4 = num < 0;
			if (flag4)
			{
				xyz *= -1.0;
			}
			return xyz;
		}

		public static Curve ConvetCurveFromCadToRevit(OdDbCurve cadCurve, Transform trans = null)
		{
			Curve result = null;
			try
			{
				string a = cadCurve.GetType().ToString();
				if (!(a == "Teigha.TD.OdDbLine"))
				{
					if (!(a == "Teigha.TD.OdDbArc"))
					{
						if (!(a == "Teigha.TD.OdDbCircle"))
						{
							if (a == "Teigha.TD.OdDbEllipse")
							{
								OdDbEllipse odDbEllipse = cadCurve as OdDbEllipse;
								OdGePoint3d odGePoint3d = odDbEllipse.center();
								OdGeVector3d odGeVector3d = odDbEllipse.majorAxis();
								OdGeVector3d odGeVector3d2 = odDbEllipse.minorAxis();
								double num = odGeVector3d.length();
								double num2 = odGeVector3d2.length();
								odGePoint3d.z = 0.0;
								XYZ center = new XYZ(odGePoint3d.x, odGePoint3d.y, 0.0) / 304.8;
								num /= 304.8;
								num2 /= 304.8;
								Curve curve = RevitDocumentExtension.HYCreateEllipse(center, num, num2, new XYZ(odGeVector3d.x, odGeVector3d.y, 0.0).Normalize(), new XYZ(odGeVector3d2.x, odGeVector3d2.y, 0.0).Normalize(), 0.0, Math.PI*2);
								result = curve;
							}
						}
						else
						{
							OdDbCircle odDbCircle = cadCurve as OdDbCircle;
							OdGePoint3d odGePoint3d2 = odDbCircle.center();
							double num3 = odDbCircle.radius();
							odGePoint3d2.z = 0.0;
							XYZ xyz = new XYZ(odGePoint3d2.x, odGePoint3d2.y, 0.0) / 304.8;
							num3 /= 304.8;
							Arc arc = Arc.Create(xyz, num3, 0.0, Math.PI*2, XYZ.BasisX, XYZ.BasisY);
							result = arc.CreateTransformed(trans);
						}
					}
					else
					{
						OdDbArc odDbArc = cadCurve as OdDbArc;
						OdGePoint3d odGePoint3d3 = odDbArc.center();
						double num4 = odDbArc.radius();
						double num5 = odDbArc.startAngle();
						double num6 = odDbArc.endAngle();
						odGePoint3d3.z = 0.0;
						XYZ xyz2 = new XYZ(odGePoint3d3.x, odGePoint3d3.y, 0.0) / 304.8;
						num4 /= 304.8;
						bool flag = num6 < num5;
						if (flag)
						{
							num6 += Math.PI*2;
						}
						Arc arc2 = Arc.Create(xyz2, num4, num5, num6, XYZ.BasisX, XYZ.BasisY);
						result = arc2.CreateTransformed(trans);
					}
				}
				else
				{
					OdDbLine odDbLine = cadCurve as OdDbLine;
					OdGePoint3d odGePoint3d4 = new OdGePoint3d();
					bool flag2 = odDbLine.getStartPoint(odGePoint3d4) > OdResult.eOk;
					if (flag2)
					{
						return null;
					}
					OdGePoint3d odGePoint3d5 = new OdGePoint3d();
					bool flag3 = odDbLine.getEndPoint(odGePoint3d5) > OdResult.eOk;
					if (flag3)
					{
						return null;
					}
					odGePoint3d4.z = 0.0;
					XYZ xyz3 = GeometryCommon.ConvetPointFromCadToRevit(odGePoint3d4, trans);
					odGePoint3d5.z = 0.0;
					XYZ xyz4 = GeometryCommon.ConvetPointFromCadToRevit(odGePoint3d5, trans);
					Line line = Line.CreateBound(xyz3, xyz4);
					result = line;
				}
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		public static string MergeNearTextNote(TextInfo textInfoRef, List<TextInfo> lstAllText)
		{
			string result = string.Empty;
			try
			{
				List<TextInfo> list = new List<TextInfo>();
				XYZ xyz = textInfoRef.ActualBox[3] - textInfoRef.ActualBox[2];
				XYZ orgPos = textInfoRef.OrgPos;
				XYZ pt = textInfoRef.OrgPos + xyz;
				foreach (TextInfo textInfo in lstAllText)
				{
					bool flag = textInfo.ActualBox.IsEmpty<XYZ>() || textInfo.ActualBox.Count != 4;
					if (!flag)
					{
						XYZ firstVec = textInfo.ActualBox[3] - textInfo.ActualBox[2];
						bool flag2 = XYZUtilityMethod.IsSameDirection(firstVec, xyz) && XYZUtilityMethod.Is_Points_Collinear(textInfo.OrgPos, orgPos, pt, 1E-09);
						if (flag2)
						{
							list.Add(textInfo);
						}
					}
				}
				List<TextInfo> list2 = new List<TextInfo>();
				list2 = list.OrderBy((TextInfo item) => item, new CompareSameLineTextPos(textInfoRef.ActualBox[3] - textInfoRef.ActualBox[2])).ToList<TextInfo>();
				List<TextInfo> list3 = new List<TextInfo>();
				int num;
				int num2;
				GeometryCommon.GetNearTextPos(textInfoRef, list2, out num, out num2);
				StringBuilder stringBuilder = new StringBuilder();
				for (int i = num; i <= num2; i++)
				{
					stringBuilder.Append(list2[i].UseText);
				}
				result = stringBuilder.ToString();
			}
			catch (Exception)
			{
			}
			return result;
		}

		private static void GetNearTextPos(TextInfo textInfoRef, List<TextInfo> lstOrderTexts, out int nStart, out int nEnd)
		{
			int num = lstOrderTexts.FindIndex((TextInfo item) => item.GuidInCad == textInfoRef.GuidInCad);
			double num2 = textInfoRef.ActualBox[0].DistanceTo(textInfoRef.ActualBox[2]) / 2.0;
			nStart = num;
			nEnd = num;
			for (int i = num - 1; i >= 0; i--)
			{
				TextInfo textInfo = lstOrderTexts[i + 1];
				TextInfo textInfo2 = lstOrderTexts[i];
				XYZ orgPos = textInfo.OrgPos;
				XYZ ptEOfLine = textInfo.OrgPos + (textInfo.ActualBox[3] - textInfo.ActualBox[2]);
				XYZ xyz = textInfo2.OrgPos + (textInfo2.ActualBox[3] - textInfo2.ActualBox[2]);
				bool flag = xyz.DistanceTo(orgPos) > num2 && !XYZUtilityMethod.Is_Point_OnSegment(orgPos, ptEOfLine, xyz, 1E-09);
				if (flag)
				{
					break;
				}
				nStart = i;
			}
			for (int j = num + 1; j < lstOrderTexts.Count; j++)
			{
				TextInfo textInfo3 = lstOrderTexts[j - 1];
				TextInfo textInfo4 = lstOrderTexts[j];
				XYZ orgPos2 = textInfo3.OrgPos;
				XYZ xyz2 = textInfo3.OrgPos + (textInfo3.ActualBox[3] - textInfo3.ActualBox[2]);
				XYZ orgPos3 = textInfo4.OrgPos;
				bool flag2 = orgPos3.DistanceTo(xyz2) > num2 && !XYZUtilityMethod.Is_Point_OnSegment(orgPos2, xyz2, orgPos3, 1E-09);
				if (flag2)
				{
					break;
				}
				nEnd = j;
			}
		}
	}
}
